#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "initProgram.h"
#include "stuManage.h"
#include "tchManage.h"

Student* student; //把文件数据存储到内存中
Teacher* teacher;

char HEADID[20] = "admin"; //校长帐号
char HEADPASSWORD[20] = "admin"; //校长密码
char HEADFLAG = 0; //校长帐号状态

int STUNUM = 0; //学生人数
int TCHNUM = 0; //教师人数

int initProgram(void)
{
	//读取学生文件
	student = calloc(STUMAX,sizeof(Student));
	FILE* stuFrp = fopen("stuList.txt","r");
	if(NULL == stuFrp)
	{
		perror("stuFopen");
		return -1;
	}
	char stuStr[sizeof(Student)] = {};
	Student stu = {};
	while(fgets(stuStr,sizeof(Student),stuFrp))
	{
		sscanf(stuStr,"%s %s %lf %lf %lf %d %c %hhd\n", stu.name, stu.password, &stu.chnScore, &stu.mathScore, &stu.engScore, &stu.id, &stu.sex, &stu.flag);
		int i = stu.id-1001;//学生学号由1001开始
		strcpy(student[i].name,stu.name);
		strcpy(student[i].password,stu.password);
		student[i].chnScore = stu.chnScore;
		student[i].mathScore = stu.mathScore;
		student[i].engScore = stu.engScore;
		student[i].id = stu.id;
		student[i].sex = stu.sex;
		student[i].flag = stu.flag;
		STUNUM++;
	}
	fclose(stuFrp);
	stuFrp = NULL;

	//读取教师文件
	teacher = calloc(TCHMAX,sizeof(Teacher));
	FILE* tchFrp = fopen("tchList.txt","r");
	if(NULL == tchFrp)
	{
		perror("tchFopen");
		return -1;
	}
	char tchStr[sizeof(Teacher)] = {};
	Teacher tch = {};
	while(fgets(tchStr,sizeof(Teacher),tchFrp))
	{
		sscanf(tchStr,"%s %s %d %c %hhd\n", tch.name, tch.password, &tch.id, &tch.sex, &tch.flag);
		int i = tch.id-101;//教师工号由101开始
		strcpy(teacher[i].name,tch.name);
		strcpy(teacher[i].password,tch.password);
		teacher[i].id = tch.id;
		teacher[i].sex = tch.sex;
		teacher[i].flag = tch.flag;
		TCHNUM++;
	}
	fclose(tchFrp);
	tchFrp = NULL;
}

